package com.boxuegu.entry;

import com.boxuegu.domain.Admin;
import com.boxuegu.domain.Fruit;
import com.boxuegu.domain.User;

import java.io.*;
import java.util.ArrayList;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;


public class IOTest {

    private static ObjectInputStream ois = null;
    private static ObjectOutputStream oos = null;

//用于初始化用户信息，水果信息，管理员信息的类
    public static void main(String[] args) throws IOException {
        initializeUser();
        initializeFruit();
        initializeAdmin();
    }

    private static void initializeAdmin() throws IOException {
        ArrayList<Admin> admins =new ArrayList<>();
        Admin admin =new Admin("boxuegu","xiwangdui");
        admins.add(admin);
        outputInAdminFile(admins);
    }

    private static void outputInAdminFile(ArrayList<Admin> admins) throws IOException {
        oos = new ObjectOutputStream(new FileOutputStream("fruit-supermarket-manager\\admin.txt"));
        if (admins!= null){
            for (Admin admin : admins) {
                oos.writeObject(admin);
            }
        }
        oos.close();
    }

    //初始化用户信息
    private static void initializeUser() throws IOException {
        ArrayList<User> users = new ArrayList<>();
        User user1 = new User("u001","zhangsan","张三",1000);
        User user2 = new User("u002","lisi","李四",100);
        users.add(user1);
        users.add(user2);
        outputInUserFile(users);
    }
//初始化水果信息
    private static void initializeFruit() throws IOException {
        ArrayList<Fruit> fruits = new ArrayList<>();
        Fruit fruit1 = new Fruit("f001","苹果",1,200);
        Fruit fruit2 = new Fruit("f002","香蕉",2,200);
        fruits.add(fruit1);
        fruits.add(fruit2);
        outputInFruitFile(fruits);
    }

    //将fruits列表的数据写到fruit.txt文件中
    public static void outputInFruitFile(ArrayList<Fruit> fruits) throws IOException {
        oos = new ObjectOutputStream(new FileOutputStream("fruit-supermarket-manager\\fruit.txt"));

        if (fruits != null) {
            for (Fruit fruit : fruits) {
                oos.writeObject(fruit);
            }
        }
        oos.close();
    }

    //将users列表的数据写到user.txt文件中
    public static void outputInUserFile(ArrayList<User> users) throws IOException {
        oos = new ObjectOutputStream(new FileOutputStream("fruit-supermarket-manager\\user.txt"));

        if (users != null) {
            for (User user : users) {
                oos.writeObject(user);
            }
        }

        oos.close();
    }


}
